package com.bjsxt.controller.erp;

import com.bjsxt.aspectj.annotation.Log;
import com.bjsxt.aspectj.enums.BusinessType;
import com.bjsxt.controller.BaseController;
import com.bjsxt.dto.MedicinesDto;
import com.bjsxt.service.MedicinesService;
import com.bjsxt.utils.ShiroSecurityUtils;
import com.bjsxt.vo.AjaxResult;
import com.bjsxt.vo.DataGridView;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author:liao
 * @Description:TODO
 * @date:2020/12/21
 */
@RestController
@RequestMapping("erp/medicines")
public class MedicinesController extends BaseController {

    @Reference
    private MedicinesService medicinesService;

    //分页查询
    @GetMapping("listMedicinesForPage")
    @HystrixCommand
    public AjaxResult listMedicinesForPage(MedicinesDto medicinesDto){
        DataGridView dataGridView = medicinesService.listMedicinesPage(medicinesDto);
        return AjaxResult.success("查询成功",dataGridView.getData(),dataGridView.getTotal());
    }

    //增加
    @PostMapping("addMedicines")
    @HystrixCommand
    @Log(title = "增加药品信息",businessType = BusinessType.INSERT)
    public AjaxResult addMedicines(@Validated MedicinesDto medicinesDto){
        medicinesDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(medicinesService.insertMedicines(medicinesDto));
    }

    //修改
    @PutMapping("updateMedicines")
    @HystrixCommand
    @Log(title = "修改药品信息",businessType = BusinessType.UPDATE)
    public AjaxResult updateMedicines(@Validated MedicinesDto medicinesDto){
        medicinesDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(medicinesService.updateMedicines(medicinesDto));
    }

    // 删除
    @DeleteMapping("deleteMedicinesByIds/{medicinesIds}")
    @Log(title = "删除药品信息",businessType = BusinessType.DELETE)
    @HystrixCommand
    public AjaxResult deleteMedicinesByIds( @PathVariable Long[] medicinesIds){
        return AjaxResult.toAjax(medicinesService.delMedicines(medicinesIds));
    }

    // 根据id查询药品信息
    @GetMapping("getMedicinesById/{medicinesId}")
    @HystrixCommand
    public AjaxResult getMedicinesById(@PathVariable Long medicinesId){
        return AjaxResult.success(medicinesService.getOne(medicinesId));
    }

    // 查询有效的药品信息
    @GetMapping("selectAllMedicines")
    @HystrixCommand
    public AjaxResult selectAllMedicines(){
        return AjaxResult.success(medicinesService.selectAllMedicines());
    }

    /**
     * 调整库存
     */
    @PostMapping("updateMedicinesStorage/{medicinesId}/{medicinesStockNum}")
    @HystrixCommand
    @Log(title = "调整药品库存",businessType = BusinessType.UPDATE)
    public AjaxResult updateMedicinesStorage(@PathVariable Long medicinesId,@PathVariable Long medicinesStockNum){
        return AjaxResult.toAjax(medicinesService.updateMedicinesStorage(medicinesId,medicinesStockNum));
    }

}
